﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using Microsoft.Phone.Shell;
using ALiteTeam.PackageTracer.Data;

namespace ALiteTeam.PackageTracer.Core
{
    public class TileHandler
    {
        /// <summary>
        /// 创建或更新Live Tile
        /// </summary>
        /// <param name="order"></param>
        /// <param name="isCreate"></param>
        public static void PinIileToScreen(DeliveryOrder order)
        {
            var navigation = string.Format("/View/Details.xaml?OrderID={0}", order.DeliveryOrderID);
            //确认是否有存在相同的Secondary Tile
            ShellTile tTileToFind = ShellTile.ActiveTiles.FirstOrDefault(
                x => x.NavigationUri.ToString().Equals(navigation));

            if (tTileToFind == null)
            {
                var data = BuildTileData(order);
                // 创建Secondary Tile
                ShellTile.Create(new Uri(navigation, UriKind.Relative), data);
            }
        }

        public static void DeleteIile(DeliveryOrder order)
        {
            var navigation = string.Format("/View/Details.xaml?OrderID={0}", order.DeliveryOrderID);

            //确认是否有存在相同的Secondary Tile
            ShellTile tTileToFind = ShellTile.ActiveTiles.FirstOrDefault(
                x => x.NavigationUri.ToString().Equals(navigation));

            if (tTileToFind != null)
            {
                tTileToFind.Delete();
            }
        }

        /// <summary>
        /// 判断一条运单是否已经被Pin到屏幕上
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public static bool Exists(DeliveryOrder order)
        {
            //确认是否有存在相同的Secondary Tile
            ShellTile tTileToFind = ShellTile.ActiveTiles.FirstOrDefault(
                x => x.NavigationUri.ToString().Contains(order.DeliveryOrderID.ToString()));

            if (tTileToFind == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        private static ShellTileData BuildTileData(DeliveryOrder order)
        {
            ShellTileData result;
            QueryResult summary = null;
            string backContent = string.Empty;

            // 若Order的Summary信息已生成
            if (order.Summary != null)
            {
                summary = QueryResult.ParseOrder(order.Summary);

                // 若查询到订单信息
                if (summary.Status == 1)
                {
                    backContent = string.Format("{0:MM-dd HH:mm}:{1}", summary.Details[0].Time, summary.Details[0].Context);
                }
                // 否则提示未查到
                else
                {
                    backContent = string.Format("{0:MM-dd HH:mm}:{1}", order.LastSummaryUpdatedTime, "暂无法查到订单信息");
                }
            }

            // 设定新建的Secondary Tile的样式
            result = new StandardTileData
            {
                BackgroundImage = new Uri("/Images/Tile/Tile.png", UriKind.Relative),
                Title = string.Format("{0}:{1}", order.DeliveryName, order.OrderNo),
                Count = 0,
                BackTitle = order.Title,
                BackContent = backContent
            };

            return result;
        }
    }
}
